package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RoleList;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R save(@RequestBody AddressBook addressBook){
        log.info("address:{}",addressBook.toString());
        //传来的JSON数据中确实userid，可以有两种方法获取
        //1. 从session中获取
        //2. 从UserHolder中获取再设置（如下）
        Long userid = UserHolder.get();
        addressBook.setUserId(userid);
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}", addressBook);
        //先将该用户的所有地址的is_default改为0
        //update address_book set is_default = 0 where user_id = ?
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId, UserHolder.get());
        //全部修改为0
        qw.set(AddressBook::getIsDefault,0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(qw);
        //再将当前设置的默认地址的is_default改为1
        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据ID查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook!= null){
            return R.success(addressBook);
        }
       return R.error("用户不存在");
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R getDefault(){
        //根据当前登录用户id以及id_default = 1查询
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.get());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(qw);
        if(addressBook != null){
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }

    /**
     * 查询指定用户的全部地址
     * @param
     * @return
     */
    @GetMapping("/list")
    public R list(){
        Long userid = UserHolder.get();
        //判断当前是否有用户登录
        //若有人登录，则获取当前用户的id
        //拿着当前用户的id，去address_book表中的user_id进行匹配
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(userid != null,AddressBook::getUserId,userid);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    /**
     * 删除地址（可能是多个）
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){//地址id
        //Long userid = UserHolder.get();
        //LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        //qw.eq(userid != null,AddressBook::getUserId,userid);
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success(null);
    }

}
